<?php
namespace Home\Controller;
use Think\Controller;

class UserController extends Controller {
	
	/*Ajax通道*/
	function doAjax(){
		$type = $_GET['type'];
		$data = $_GET['data'];
		
		$returnMsg = "default Massage!";
		
		switch($type){
			case "feedback":
				$returnMsg = $this->feedBack($data);
			break;
			
			default:
			break;
		}
		
		$this->ajaxReturn($returnMsg);
	}
	
	/*反馈写入*/
	function feedBack($data){

		$data = explode("///", $data);
		
		if(session('nav_valiCode') == "")
			session('nav_valiCode',rand(10000,99999));
		
		if($data[1] == session('nav_valiCode')){
			
			$data[0] = strip_tags($data[0]);
			$status = writeLog($data[0], 'feedback');
			
			if($status){
				return "反馈成功,写信您的支持!";
			}else{
				return "抱歉,反馈失败,请重试!";
			}
		}else{
			return "抱歉,反馈失败,请重试!";
		}
		
	}
	
	/* 用户登录 */
	function toLogin() {
		if (! isset ( $_POST ['login_email'] )) {
			$this->display ( "/User/toLogin_1" );
		} else {
			$email = $_POST ['login_email'];
			$password = $_POST ['login_password'];
			$remeberMe = $_POST ['login_remeberMe'];
			
			/*
			$verifyCode = $_POST ['login_verify'];
			
			$verify = new \Think\Verify ();
			$result = $verify->check ( $verifyCode );
			if (! $result)
				$this->error ( '验证码错误!' );
			*/
			
			$data = array (
					"email" => $email,
					"password" => $password,
					"remeberMe" => $remeberMe 
			);
			
			$redis = getObj ( 'redis' );
			$status = $redis->toLogin ( $data );
			if ($status == 1) {
				if ($remeberMe == "yes") {
					cookie ( 'nwb_userid', session( 'nwb_userid' ), C( 'cookie_time' ) );
					cookie ( 'nwb_email', $data['email'], C( 'cookie_time' ) );
					cookie ( 'nwb_password', $data ['password'], C( 'cookie_time' ) );
					cookie ( 'nwb_usercen', session( 'nwb_usercen' ), C( 'cookie_time' ) );
				}
				
				$this->redirect ( "/Home/Index/toStart" );
			} else {
				$this->error ( C( 'exe_code.' . $status ) );
			}
		}
	}
	
	/* 用户注册 */
	function toSignup() {
		if (! isset ( $_POST ['signup_email'] )) {
			$this->display ( "/User/toSignup_1" );
		} else {
			$email = $_POST ['signup_email'];
			$password = $_POST ['signup_password'];
			$data = array (
					"email" => $email,
					"password" => $password 
			);
			$redis = getObj ( 'redis' );
			$result = $redis->toSignup ( $data );
			if ($result == 1) {
				$this->redirect ( "/Home/Index/toStart" );
			} else {
				$this->error ( C ( 'exe_code.' . $result ) );
			}
		}
	}
	
	/* 退出登录 */
	function toLogout() {
		cookie ( 'nwb_userid', null );
		cookie ( 'nwb_email', null );
		cookie ( 'nwb_usercen', null );
		
		session ( 'nwb_islogin', null );
		session ( 'nwb_userid', 10000 );
		session ( 'nwb_email', 'visitor' );
		session ( 'nwb_usercen', null );
		$this->redirect ( "/Home/Index/toStart" );
	}
	
	/* 重置密码 */
	function rePwd() {
		$old_pwd = strip_tags ( $_POST ['rePwd_old_pwd'] );
		$new_pwd = strip_tags ( $_POST ['rePwd_new_pwd'] );
		$data = array (
				'old_pwd' => $old_pwd,
				'new_pwd' => $new_pwd 
		);
		$redis = getObj ( 'redis' );
		$result = $redis->rePwd ( $data );
		
		if ($result == 1) {
			$this->redirect ( "/Home/Index/toStart" );
		} else {
			$this->error ( C ( 'exe_code.' . $result ) );
		}
	}
}